package com.zdkj.subway.check.controller.subwayRat.conductorDifferences.passengersTransaction;

import com.alibaba.fastjson.JSONObject;
import com.zdkj.subway.check.entity.*;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.*;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.util.DateFormat;
import com.zdkj.subway.check.util.JdbcUtil;
import com.zdkj.subway.check.util.UserInfo;
import io.swagger.annotations.ApiOperation;
import com.zdkj.common.core.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 乘客事务审核
 */
@RestController
@RequestMapping("/check")
@Slf4j
public class TransactionAuditController {

    @Autowired
    private SwStInputPassengerWorkMapper swStInputPassengerWorkMapper;
    @Autowired
    private SwStInputPassengerWorkCashMapper swStInputPassengerWorkCashMapper;
    @Autowired
    private SwStInputPassengerWorkElectronMapper swStInputPassengerWorkElectronMapper;
    @Autowired
    private SwStInputPassengerWorkPayMapper swStInputPassengerWorkPayMapper;
    @Autowired
    private SwStInputPassengerWorkSpecialMapper swStInputPassengerWorkSpecialMapper;
    @Autowired
    private SwStInputPassengerWorkFreeMapper swStInputPassengerWorkFreeMapper;
    @Autowired
    private SwStInputPassengerWorkRefundMapper swStInputPassengerWorkRefundMapper;
    @Autowired
    private SwStInputPassengerWorkTicketMapper swStInputPassengerWorkTicketMapper;
    @Autowired
    private SwStInputPassengerWorkOtherMapper swStInputPassengerWorkOtherMapper;
    @Autowired
    private SwAuPassengerWorkLogMapper swAuPassengerWorkLogMapper;
    @Autowired
    private SwAuCenterConductorAmountDifferenceMapper swAuCenterConductorAmountDifferenceMapper;
    
	/**
	  
	 现金事务
	*/
    @RequestMapping("/getSwStInputPassengerWorkCash")
    @ApiOperation(value = "现金事务", notes = "现金事务")
    public Page getSwStInputPassengerWorkCash(Page page,@RequestParam Map<String,String> param){
        List<Map<String, Object>> swStInputPassengerWorkCash = swStInputPassengerWorkCashMapper.getSwStInputPassengerWorkCash(param);
        page.setData(swStInputPassengerWorkCash);
        return page;
    }
    /**
	  
	 电子事务
	*/
    @RequestMapping("/getSwStInputPassengerWorkElectron")
    @ApiOperation(value = "电子事务", notes = "电子事务")
    public Page getSwStInputPassengerWorkElectron(Page page,@RequestParam Map<String,String> param){
        List<Map<String, Object>> swStInputPassengerWorkElectron = swStInputPassengerWorkElectronMapper.getSwStInputPassengerWorkElectron(param);
        page.setData(swStInputPassengerWorkElectron);
        return page;
    }

    /**
     * 付费出站
     * @param page
     * @param param
     * @return
     */
    @RequestMapping("/getSwStInputPassengerWorkPay")
    @ApiOperation(value = "付费出站", notes = "付费出站")
    public Page getSwStInputPassengerWorkPay(Page page,@RequestParam Map<String,String> param) throws ParseException {
        List<Map<String, Object>> swStInputPassengerWorkPay = swStInputPassengerWorkPayMapper.getSwStInputPassengerWorkPay(param);
        String row = param.get("row");
        JSONObject jsonObject = JSONObject.parseObject(row);
        String bookdate= DateFormat.ex_strDate_to_date(jsonObject.getString("workDate"),"yyyyMMdd");
        String lineCode=jsonObject.getString("lineCode");
        String stationCode=jsonObject.getString("stationCode");
       /*
		try{
            for(Map<String, Object> map:swStInputPassengerWorkPay){
                String sql="select sum(sale_carnum) as num,sum(sale_txnamtall) as amount from sc_fare_statistics where productType=1 " +
                        "and bookdate='"+bookdate+"'" +
                        "and lineid='"+lineCode+"'" +
                        "and station_number='"+stationCode+"'" ;
//                "and station_equipment_number='"++"'" +
//                "and posoperid=''";
                List<Map<String, Object>> maps = JdbcUtil.exeSqlQuery(sql);
                if(maps.size()>0){
                    map.put("sc_amount",maps.get(0).get("amount"));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }*/
        page.setData(swStInputPassengerWorkPay);
        return page;
    }
    /*
	  特殊车票
	*/
    @RequestMapping("/getSwStInputPassengerWorkSpecial")
    @ApiOperation(value = "特殊车票", notes = "特殊车票")
    public Page getSwStInputPassengerWorkSpecial(Page page,@RequestParam Map<String,String> param){
        List<Map<String, Object>> swStInputPassengerWorkSpecial = swStInputPassengerWorkSpecialMapper.getSwStInputPassengerWorkSpecial(param);
        page.setData(swStInputPassengerWorkSpecial);
        return page;
    }
    /*	
	  免费出站票 ，前台去掉SC显示
	  */
    @RequestMapping("/getSwStInputPassengerWorkFree")
    @ApiOperation(value = "免费出站票 ，前台去掉SC显示", notes = "免费出站票 ，前台去掉SC显示")
    public Page getSwStInputPassengerWorkFree(Page page,@RequestParam Map<String,String> param) throws ParseException {
        List<Map<String, Object>> swStInputPassengerWorkFree = swStInputPassengerWorkFreeMapper.getSwStInputPassengerWorkFree(param);
        String row = param.get("row");
        JSONObject jsonObject = JSONObject.parseObject(row);
        String bookdate= DateFormat.ex_strDate_to_date(jsonObject.getString("workDate"),"yyyyMMdd");
        String lineCode=jsonObject.getString("lineCode");
        String stationCode=jsonObject.getString("stationCode");
		/*
        try{
            for(Map<String, Object> map:swStInputPassengerWorkFree){
                String sql="select sum(sale_carnum) as num,sum(sale_txnamtall) as amount from sc_fare_statistics where productType=1 " +
                        "and bookdate='"+bookdate+"'" +
                        "and lineid='"+lineCode+"'" +
                        "and station_number='"+stationCode+"'" ;
//                "and station_equipment_number='"++"'" +
//                "and posoperid=''";
                List<Map<String, Object>> maps = JdbcUtil.exeSqlQuery(sql);
                if(maps.size()>0){
                    map.put("sc_amount",maps.get(0).get("amount"));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }*/
        page.setData(swStInputPassengerWorkFree);
        return page;
    }
    /*	
	  故障退款
	  */
    @RequestMapping("/getSwStInputPassengerWorkRefund")
    @ApiOperation(value = "故障退款", notes = "故障退款")
    public Page getSwStInputPassengerWorkRefund(Page page,@RequestParam Map<String,String> param){
        List<Map<String, Object>> swStInputPassengerWorkRefund = swStInputPassengerWorkRefundMapper.getSwStInputPassengerWorkRefund(param);
        page.setData(swStInputPassengerWorkRefund);
        return page;
    }
     /*
	  即时退换票
	*/
    @RequestMapping("/getSwStInputPassengerWorkTicket")
    @ApiOperation(value = "即时退换票", notes = "即时退换票")
    public Page getSwStInputPassengerWorkTicket(Page page,@RequestParam Map<String,String> param){
        List<Map<String, Object>> swStInputPassengerWorkTicket = swStInputPassengerWorkTicketMapper.getSwStInputPassengerWorkTicket(param);
        page.setData(swStInputPassengerWorkTicket);
        return page;
    }
    /*
	  其他相关数据录入
	*/
    @RequestMapping("/getSwStInputPassengerWorkOther")
    @ApiOperation(value = "其他相关数据录入", notes = "其他相关数据录入")
    public Page swStInputPassengerWorkOtherOption(Page page,@RequestParam Map<String,String> param){
        List<Map<String, Object>> swStInputPassengerWorkTicket = swStInputPassengerWorkOtherMapper.getSwStInputPassengerWorkOther(param);
        page.setData(swStInputPassengerWorkTicket);
        return page;
    }
   /*
	  查看各个数据项是否有值，前台进行不同颜色区分
	*/
    @RequestMapping("/queryCKSWCount")
    @ApiOperation(value = "查看各个数据项是否有值，前台进行不同颜色区分", notes = "查看各个数据项是否有值，前台进行不同颜色区分")
    public Map queryCKSWCount(@RequestParam Map<String,String> param){
        Map req = new HashMap();
        Long relationId = Long.valueOf(param.get("id"));
        req.put("relationId",relationId);
        //乘客事务录入明细表【现金事务】
        List<Map<String, Object>> swStInputPassengerWorkCash1 = swStInputPassengerWorkCashMapper.getSwStInputPassengerWorkCash(req);
        //乘客事务录入明细表【电子事务】
        List<Map<String, Object>> swStInputPassengerWorkElectron1 = swStInputPassengerWorkElectronMapper.getSwStInputPassengerWorkElectron(req);
        //乘客事务录入明细表【付费出站票发售】
        List<Map<String, Object>> swStInputPassengerWorkPay1 = swStInputPassengerWorkPayMapper.getSwStInputPassengerWorkPay(req);
        //乘客事务录入明细表【特殊车票发售】
        List<Map<String, Object>> swStInputPassengerWorkSpecial1 = swStInputPassengerWorkSpecialMapper.getSwStInputPassengerWorkSpecial(req);
        //乘客事务录入明细表【免费出站票发售】
        List<Map<String, Object>> swStInputPassengerWorkFree1 = swStInputPassengerWorkFreeMapper.getSwStInputPassengerWorkFree(req);
        //乘客事务录入明细表【故障退款】
        List<Map<String, Object>> swStInputPassengerWorkRefund1 = swStInputPassengerWorkRefundMapper.getSwStInputPassengerWorkRefund(req);
        req.put("handlePersonCode",param.get("conductorCode"));
        req.put("handleTime",param.get("handleTime"));
        req.put("lineCode",String.valueOf(param.get("lineCode")));
        req.put("stationCode",String.valueOf(param.get("stationCode")));
        req.put("operationType","0");
        //乘客事务退换票
        List<Map<String, Object>> swStInputPassengerWorkTicket1 = swStInputPassengerWorkTicketMapper.getSwStInputPassengerWorkTicket(req);
        req.put("operationType","1");
        //乘客事务退换票
        List<Map<String, Object>> swStInputPassengerWorkTicket = swStInputPassengerWorkTicketMapper.getSwStInputPassengerWorkTicket(req);
        req.put("conductorCode",param.get("conductorCode"));
        req.put("inputTime",param.get("handleTime"));
        //其他相关数据录入
        List<Map<String, Object>> swStInputPassengerWorkOther1 = swStInputPassengerWorkOtherMapper.getSwStInputPassengerWorkOther(req);
        Map resp = new HashMap();
        resp.put("swStInputPassengerWorkCash",swStInputPassengerWorkCash1.size());
        resp.put("swStInputPassengerWorkElectron",swStInputPassengerWorkElectron1.size());
        resp.put("swStInputPassengerWorkPay",swStInputPassengerWorkPay1.size());
        resp.put("swStInputPassengerWorkSpecial",swStInputPassengerWorkSpecial1.size());
        resp.put("swStInputPassengerWorkFree",swStInputPassengerWorkFree1.size());
        resp.put("swStInputPassengerWorkRefund",swStInputPassengerWorkRefund1.size());
        resp.put("swStInputPassengerWorkTicket1",swStInputPassengerWorkTicket1.size());
        resp.put("swStInputPassengerWorkTicket",swStInputPassengerWorkTicket.size());
        resp.put("swStInputPassengerWorkOther",swStInputPassengerWorkOther1.size());
        return resp;
    }

	/*
	  获取SC层级的付费出站票和免费出站票的数量以及金额
	*/
    @RequestMapping("/queryScTicketCountAndMoney")
    @ApiOperation(value = "获取SC层级的付费出站票和免费出站票的数量以及金额", notes = "获取SC层级的付费出站票和免费出站票的数量以及金额")
    public Map queryScTicketCountAndMoney(@RequestParam Map<String,String> param){
        Map req = new HashMap();
        //Long relationId = Long.valueOf(param.get("id"));
		String lineCode = String.valueOf(param.get("lineCode"));  
		String stationCode = String.valueOf(param.get("stationCode"));
		String workDate = String.valueOf(param.get("handleTime"));//事务日期
		
		//String conductorCode = String.valueOf(param.get("conductorCode"));//售票员编号
		String deviceOperateNum = String.valueOf(param.get("deviceOperateNum"));//设备操作号
		String bomCode = String.valueOf(param.get("bomCode"));//BOM编号
		String bomin= "";
		if(bomCode!=null && bomCode!=""){
		    String[] split = (bomCode.replaceAll(", ", ",")).split(",");
		    bomin="'"+org.apache.commons.lang.StringUtils.join(split, "','")+"'";
		}
	
        try{
               Object startTime = DateFormat.ex_strDate_to_date(workDate,"yyyyMMdd");
			   String sql ="select ifnull(sum(transactionNumber),0) as num,ifnull(sum(transactionValue)/100,0) as amount from sc_out_ticket  where equipmenttype='2' and tickettype='0' " +          
                        " and bookdate='"+workDate+"'" +
                        " and lineid='"+lineCode+"'" +
                        " and stationid='"+stationCode+"'" +
						" and station_equipment_number in ("+bomin+")"+
                        " and posoperid like '%"+deviceOperateNum+"'";

                List<Map<String, Object>> maps = JdbcUtil.exeSqlQuery(sql);
                if(maps.size()>0){
					 req.put("sc_free_num",maps.get(0).get("num"));
                     req.put("sc_free_amount",maps.get(0).get("amount"));
                }else{
				     req.put("sc_free_num",0);//免费出站票
                     req.put("sc_free_amount",0.00);
				}

				String sqlSpecial ="select ifnull(sum(sale_carnum),0) as num from sc_fare_statistics  where productType =12 " +          
                        " and bookdate='"+startTime+"'" +
                        " and lineid='"+lineCode+"'" +
                        " and station_number='"+stationCode+"'" +
						//" and station_equipment_number in ("+bomin+")"+
                        " and posoperid like '%"+deviceOperateNum+"'";

                List<Map<String, Object>> mapsSpecial = JdbcUtil.exeSqlQuery(sqlSpecial);
                if(mapsSpecial.size()>0){
					 req.put("sc_special_num",mapsSpecial.get(0).get("num"));                    
                }else{
				     req.put("sc_special_num",0);//特殊发售车票（福利票）                    
				}



				 String sqlMoney ="select ifnull(sum(transactionNumber),0) as num,ifnull(sum(transactionValue)/100,0) as amount from sc_out_ticket  where equipmenttype='2' and tickettype='1' "+           
                        " and bookdate='"+workDate+"'" +
                        " and lineid='"+lineCode+"'" +
                        " and stationid='"+stationCode+"'" +
						" and station_equipment_number in ("+bomin+")"+
                        " and posoperid like '%"+deviceOperateNum+"'";

                List<Map<String, Object>> mapMoney = JdbcUtil.exeSqlQuery(sqlMoney);
                if(mapMoney.size()>0){
					 req.put("sc_money_num",mapMoney.get(0).get("num"));
                     req.put("sc_money_amount",mapMoney.get(0).get("amount"));
                }else{
				     req.put("sc_money_num",0);
                     req.put("sc_money_amount",0.00);
				}
           
        }catch (Exception e){
            e.printStackTrace();
        }  
		if(req==null || req.size()==0){
		   req.put("sc_free_num",0);
           req.put("sc_free_amount",0.00);
		   req.put("sc_money_num",0);
           req.put("sc_money_amount",0.00);		
		}
     
        return req;
    }
	/*
	
	  乘客事务审核不通过时，加入票款差异过程
	  20201229 改为只有现金和故障退款加入票款差异重新计算公式
	  20210506 增加其他相关数据录入的票款差异重新计算
	*/

    @RequestMapping("/updateTransactionAuditToPKCY")
    @ApiOperation(value = "乘客事务审核不通过时，加入票款差异过程", notes = "乘客事务审核不通过时，加入票款差异过程")
    public Result updateTransactionAuditToPKCY(@RequestParam Map<String,String> param) throws SQLException, ParseException{
        Result res=new Result(ResultDetailMsg.OK);
        String lineCode = String.valueOf(param.get("lineCode"));
        String stationCode = String.valueOf(param.get("stationCode"));
        String workDate = String.valueOf(param.get("workDate"));
        String conductorCode = String.valueOf(param.get("conductorCode"));
		String classNumber = String.valueOf(param.get("classNumber"));
		
		
        Map req = new HashMap();
        req.put("lineCode",lineCode);
        req.put("stationCode",stationCode);
        req.put("workDate",workDate);
        req.put("conductorJobNo",conductorCode);
        List<Map<String, Object>> swAuCenterConductorAmountDifferenceListToTime = swAuCenterConductorAmountDifferenceMapper.getSwAuCenterConductorAmountDifferenceListToTime(req);
        if(null == swAuCenterConductorAmountDifferenceListToTime || 0 == swAuCenterConductorAmountDifferenceListToTime.size()){
            res = new Result(ResultDetailMsg.CHECK_SELECT_ERROR,"还未到加入票款差异时机");
            return res;
        }
        Map<String, Object> map = swAuCenterConductorAmountDifferenceListToTime.get(0);
        String incomeSettleBeginTime = String.valueOf(map.get("incomeSettleBeginTime"));
        String incomeSettleEndTime = String.valueOf(map.get("incomeSettleEndTime"));
        req.remove("workDate");
        req.put("conductorCode",conductorCode);
        req.put("incomeSettleBeginTime",incomeSettleBeginTime);
        req.put("incomeSettleEndTime",incomeSettleEndTime);


		Map reqOther = new HashMap();  //查找其他相关数据录入参数
		reqOther.put("lineCode",lineCode);
        reqOther.put("stationCode",stationCode);       
        reqOther.put("conductorJobNo",conductorCode);
		reqOther.put("conductorCode",conductorCode);
        reqOther.put("incomeSettleBeginTime",incomeSettleBeginTime);
        reqOther.put("incomeSettleEndTime",incomeSettleEndTime);
		reqOther.put("deviceOperateNum",String.valueOf(map.get("equipmentNumber")));		
		reqOther.put("classNumber",classNumber);


		//乘客事务录入主表
        List<Map<String, Object>> swStInputPassengerWork = swStInputPassengerWorkMapper.getSwStInputPassengerWork(req);
        List<Map<String, Object>> allList = new ArrayList();
        for(Map m : swStInputPassengerWork){
            Long id = Long.valueOf(m.get("id").toString());
            req.clear();
            req.put("relationId",id);
            //乘客事务录入明细表【现金事务】
            List<Map<String, Object>> swStInputPassengerWorkCash = swStInputPassengerWorkCashMapper.getSwStInputPassengerWorkCash(req);
            //List<Map<String, Object>> swStInputPassengerWorkElectron = swStInputPassengerWorkElectronMapper.getSwStInputPassengerWorkElectron(req);
            //List<Map<String, Object>> swStInputPassengerWorkPay = swStInputPassengerWorkPayMapper.getSwStInputPassengerWorkPay(req);

           //乘客事务录入明细表【故障退款】
            List<Map<String, Object>> swStInputPassengerWorkRefund = swStInputPassengerWorkRefundMapper.getSwStInputPassengerWorkRefund(req);
            req.clear();
            req.put("handlePersonCode",String.valueOf(m.get("conductorCode")));
            req.put("handleTime",String.valueOf(m.get("workDate")));
            req.put("lineCode",String.valueOf(m.get("lineCode")));
            req.put("stationCode",String.valueOf(m.get("stationCode")));
            //List<Map<String, Object>> swStInputPassengerWorkTicket = swStInputPassengerWorkTicketMapper.getSwStInputPassengerWorkTicket(req);
            req.put("conductorCode",String.valueOf(m.get("conductorCode")));
            req.put("inputTime",String.valueOf(m.get("workDate")));
            //查找 其他相关数据录入表的data_type=2和3的所有数据  以及data_type=1,并且happen_cause=2 和5 的数据
			List<Map<String, Object>> swStInputPassengerWorkOther = swStInputPassengerWorkOtherMapper.getSwStInputPassengerWorkOtherAmount(reqOther);
			


            allList.addAll(swStInputPassengerWorkCash);
            //allList.addAll(swStInputPassengerWorkElectron);
            //allList.addAll(swStInputPassengerWorkPay);
            allList.addAll(swStInputPassengerWorkRefund);
            //allList.addAll(swStInputPassengerWorkTicket);
            allList.addAll(swStInputPassengerWorkOther);
        }
        BigDecimal allAmount = new BigDecimal(0);
        for(Map m : allList){
            if(null != m.get("auditAmount")){
                allAmount = allAmount.add((BigDecimal)m.get("auditAmount"));
            }else if(null != m.get("amount")){
                allAmount = allAmount.add((BigDecimal)m.get("amount"));
            }else if(null != m.get("amountRefunded")){
                allAmount = allAmount.add((BigDecimal)m.get("amountRefunded"));
            }
        }
        allAmount = allAmount.multiply(new BigDecimal(100));
        SwAuCenterConductorAmountDifference swAuCenterConductorAmountDifference = JSONObject.parseObject(JSONObject.toJSONString(map), SwAuCenterConductorAmountDifference.class);
        
        
//		Map reqSingleTicket = new HashMap(); //查找预制单程票参数
//		reqSingleTicket.put("lineCode",lineCode);
//        reqSingleTicket.put("stationCode",stationCode);
//        reqSingleTicket.put("conductorJobNo",conductorCode);
//		reqSingleTicket.put("conductorCode",conductorCode);
//        reqSingleTicket.put("startTime",incomeSettleBeginTime);//日期字符串格式，需要小时分秒  02:00:00
//        reqSingleTicket.put("endTime",incomeSettleEndTime);//日期字符串格式，需要小时分秒      01:59:59
//		reqSingleTicket.put("conductorShifts",classNumber);
//		reqSingleTicket.put("deviceOperateNum",String.valueOf(map.get("equipmentNumber")));
//		BigDecimal singleTicketAmount = BomUtil.getSingleTicketAmountByMap(reqSingleTicket);//单程票
//
//		if(singleTicketAmount!=null){
//            allAmount = allAmount.add(singleTicketAmount);//乘客事务金额需要另加上配票中的预制单程票
//        }

		swAuCenterConductorAmountDifference.setPassengerWorkAmount(allAmount);
        
		//差异金额需要重新计算---TODO  20210506
		swAuCenterConductorAmountDifference.setDifferenceAmount(
                swAuCenterConductorAmountDifference.getPaidAmount().subtract(swAuCenterConductorAmountDifference.getSysAmount()).subtract(allAmount)
                        .subtract(swAuCenterConductorAmountDifference.getMarketTicketAmount())
                        .subtract(swAuCenterConductorAmountDifference.getSingleTicketAmount())
			            .subtract(swAuCenterConductorAmountDifference.getEmergencyAmount()));
        swAuCenterConductorAmountDifferenceMapper.updateById(swAuCenterConductorAmountDifference);
        return res;
    }

    /**
     * 审批不通过
     * @param param
     * @return
     */
    @RequestMapping("/updateTransactionAuditToF")
    @ApiOperation(value = "审批不通过", notes = "审批不通过")
    @Transactional(rollbackFor = Exception.class)
    public Result updateTransactionAuditToF(@RequestParam Map<String,String> param){
        Long id = Long.valueOf(param.get("id"));
        Long parentId = Long.valueOf(param.get("parentId"));
        String lineCode = String.valueOf(param.get("lineCode"));
        String stationCode = String.valueOf(param.get("stationCode"));
        String conductorName = String.valueOf(param.get("conductorName"));
        String conductorCode = String.valueOf(param.get("conductorCode"));
        String type = String.valueOf(param.get("relationtype"));
        String reason = String.valueOf(param.get("reason"));
        String auditAmount = String.valueOf(param.get("auditAmount"));
        try{
            SwStInputPassengerWork swStInputPassengerWork = swStInputPassengerWorkMapper.selectById(parentId);



            Map req = new HashMap();
            List<Map<String, Object>> allList = new ArrayList();
            Long relationId = swStInputPassengerWork.getId();
            req.put("relationId",relationId);
            //乘客事务录入明细表【现金事务】
            List<Map<String, Object>> swStInputPassengerWorkCash1 = swStInputPassengerWorkCashMapper.getSwStInputPassengerWorkCash(req);
            //乘客事务录入明细表【电子事务】
            List<Map<String, Object>> swStInputPassengerWorkElectron1 = swStInputPassengerWorkElectronMapper.getSwStInputPassengerWorkElectron(req);
            //乘客事务录入明细表【付费出站票发售】
            List<Map<String, Object>> swStInputPassengerWorkPay1 = swStInputPassengerWorkPayMapper.getSwStInputPassengerWorkPay(req);
            //乘客事务录入明细表【特殊车票发售】
            List<Map<String, Object>> swStInputPassengerWorkSpecial1 = swStInputPassengerWorkSpecialMapper.getSwStInputPassengerWorkSpecial(req);
            //乘客事务录入明细表【免费出站票发售】
            List<Map<String, Object>> swStInputPassengerWorkFree1 = swStInputPassengerWorkFreeMapper.getSwStInputPassengerWorkFree(req);
            //乘客事务录入明细表【故障退款】
            List<Map<String, Object>> swStInputPassengerWorkRefund1 = swStInputPassengerWorkRefundMapper.getSwStInputPassengerWorkRefund(req);
            req.clear();
            Date workDate = swStInputPassengerWork.getWorkDate();
            SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd");
            String format = dff.format(workDate);
            req.put("handlePersonCode",swStInputPassengerWork.getConductorCode());
            req.put("handleTime",format);
            req.put("lineCode",String.valueOf(param.get("lineCode")));
            req.put("stationCode",String.valueOf(param.get("stationCode")));
            //乘客事务退换票
            List<Map<String, Object>> swStInputPassengerWorkTicket1 = swStInputPassengerWorkTicketMapper.getSwStInputPassengerWorkTicket(req);
            req.put("conductorCode",swStInputPassengerWork.getConductorCode());
            req.put("inputTime",format);
            //其他相关数据录入
            List<Map<String, Object>> swStInputPassengerWorkOther1 = swStInputPassengerWorkOtherMapper.getSwStInputPassengerWorkOther(req);
            allList.addAll(swStInputPassengerWorkCash1);
            allList.addAll(swStInputPassengerWorkElectron1);
            allList.addAll(swStInputPassengerWorkPay1);
            allList.addAll(swStInputPassengerWorkSpecial1);
            allList.addAll(swStInputPassengerWorkFree1);
            allList.addAll(swStInputPassengerWorkRefund1);
            allList.addAll(swStInputPassengerWorkTicket1);
            allList.addAll(swStInputPassengerWorkOther1);
            int count = 0;
            for(Map<String, Object> m : allList){
                String auditStatus = String.valueOf(m.get("auditStatus"));
                if((null == auditStatus || "1".equals(auditStatus)) && !relationId.equals(Long.valueOf(String.valueOf(m.get("id"))))){
                    count++;
                    break;
                }
            }


            //判断如果主表状态为未审批，并且子表状态没有未审批的
            if(null != swStInputPassengerWork && "1".equals(swStInputPassengerWork.getAuditStatus()) && 0==count){
                swStInputPassengerWork.setAuditStatus("3");
                swStInputPassengerWorkMapper.updateById(swStInputPassengerWork);
            }
            this.updateById(id,type,"3");
            SwAuPassengerWorkLog swAuPassengerWorkLog = new SwAuPassengerWorkLog();
            swAuPassengerWorkLog.setRelationid(Integer.valueOf(param.get("id")));
            swAuPassengerWorkLog.setRelationtype(type);
            String amoutType = "";
            switch (type) {
                case "1": //乘客事务录入明细表【现金事务】
                    SwStInputPassengerWorkCash swStInputPassengerWorkCash = swStInputPassengerWorkCashMapper.selectById(id);
                    swAuPassengerWorkLog.setOldAmount(swStInputPassengerWorkCash.getAmount());
                    amoutType = "1";
                    break;
                case "2": //乘客事务录入明细表【电子事务】
                    SwStInputPassengerWorkElectron swStInputPassengerWorkElectron = swStInputPassengerWorkElectronMapper.selectById(id);
                    swAuPassengerWorkLog.setOldAmount(swStInputPassengerWorkElectron.getAmount());
                    amoutType = "1";
                    break;
                case "3": //乘客事务录入明细表【付费出站票发售】
                    SwStInputPassengerWorkPay swStInputPassengerWorkPay = swStInputPassengerWorkPayMapper.selectById(id);
                    swAuPassengerWorkLog.setOldAmount(swStInputPassengerWorkPay.getAmount());
                    amoutType = "1";
                    break;
                case "4": //乘客事务录入明细表【特殊车票发售】
                    SwStInputPassengerWorkSpecial swStInputPassengerWorkSpecial = swStInputPassengerWorkSpecialMapper.selectById(id);
                    swAuPassengerWorkLog.setOldNumber(BigDecimal.valueOf(swStInputPassengerWorkSpecial.getSellNumber()));
                    amoutType = "2";
                    break;
                case "5": //乘客事务录入明细表【免费出站票发售】
                    SwStInputPassengerWorkFree swStInputPassengerWorkFree = swStInputPassengerWorkFreeMapper.selectById(id);
                    swAuPassengerWorkLog.setOldNumber(BigDecimal.valueOf(swStInputPassengerWorkFree.getTotal()));
                    amoutType = "2";
                    break;
                case "6": //乘客事务录入明细表【故障退款】
                    SwStInputPassengerWorkRefund swStInputPassengerWorkRefund = swStInputPassengerWorkRefundMapper.selectById(id);
                    swAuPassengerWorkLog.setOldAmount(swStInputPassengerWorkRefund.getAmountRefunded());
                    amoutType = "1";
                    break;
                case "7": //乘客事务退换票
                    SwStInputPassengerWorkTicket swStInputPassengerWorkTicket = swStInputPassengerWorkTicketMapper.selectById(id);
                    swAuPassengerWorkLog.setOldAmount(swStInputPassengerWorkTicket.getAmount());
                    amoutType = "1";
                    if("1".equals(swStInputPassengerWorkTicket.getOperationType())){
                        swAuPassengerWorkLog.setRelationtype("8");
                    }
                    break;
                case "8": //其他相关数据录入
                    SwStInputPassengerWorkOther swStInputPassengerWorkOther = swStInputPassengerWorkOtherMapper.selectById(id);
                    swAuPassengerWorkLog.setOldAmount(swStInputPassengerWorkOther.getAmount());
                    amoutType = "1";
                    break;
            }
            swAuPassengerWorkLog.setAmoutType(amoutType);
            swAuPassengerWorkLog.setAuditAmount(BigDecimal.valueOf(Long.valueOf(auditAmount)));
            swAuPassengerWorkLog.setAuditNumber(BigDecimal.valueOf(Long.valueOf(auditAmount)));
            swAuPassengerWorkLog.setReason(reason);
            swAuPassengerWorkLog.setLineCode(lineCode);
            swAuPassengerWorkLog.setStationCode(stationCode);
            swAuPassengerWorkLog.setConductorPersonName(conductorName);
            swAuPassengerWorkLog.setConductorPersonCode(conductorCode);
            swAuPassengerWorkLog.setCreatorId(UserInfo.getId());
            swAuPassengerWorkLog.setCreatorName(UserInfo.getUserName());
            swAuPassengerWorkLog.setCreateTime(new Date());
            swAuPassengerWorkLogMapper.insert(swAuPassengerWorkLog);
        }catch (Exception e){
            log.error("{}",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        Result res=new Result(ResultDetailMsg.OK);
        return res;
    }
    @RequestMapping("/updateAuditRemark")
    @ApiOperation(value = "乘客事务审核增加审核意见备注", notes = "乘客事务审核增加审核意见备注")
    public R updateAuditRemark(@RequestBody JSONObject jsonObject){
		
		Map param = JSONObject.parseObject(jsonObject.toString(),Map.class);
		Long id = Long.valueOf(String.valueOf(param.get("id")));
		String flag = String.valueOf(param.get("flag"));
		String remark = String.valueOf(param.get("remark"));

		if("1".equals(flag)){
			SwStInputPassengerWorkPay swStInputPassengerWorkPay1 = new SwStInputPassengerWorkPay();
            swStInputPassengerWorkPay1.setId(Long.valueOf(id));
            swStInputPassengerWorkPay1.setAuditRemark(remark);
            swStInputPassengerWorkPayMapper.updateById(swStInputPassengerWorkPay1);

		}
		if("2".equals(flag)){
			SwStInputPassengerWorkSpecial swStInputPassengerWorkSpecial1 = new SwStInputPassengerWorkSpecial();
            swStInputPassengerWorkSpecial1.setId(Long.valueOf(id));
            swStInputPassengerWorkSpecial1.setAuditRemark(remark);
            swStInputPassengerWorkSpecialMapper.updateById(swStInputPassengerWorkSpecial1);

		}
		if("3".equals(flag)){
			 SwStInputPassengerWorkFree swStInputPassengerWorkFree1 = new SwStInputPassengerWorkFree();
             swStInputPassengerWorkFree1.setId(Long.valueOf(id));
             swStInputPassengerWorkFree1.setAuditRemark(remark);
             swStInputPassengerWorkFreeMapper.updateById(swStInputPassengerWorkFree1);
		}
       
        return R.ok();
    }
    /**
     * 审批通过
     * @param param
     * @return
     */
    @RequestMapping("/updateTransactionAudit")
    @ApiOperation(value = "审批通过", notes = "审批通过")
    public Result updateTransactionAudit(@RequestParam Map<String,String> param){
        Map req = new HashMap();
        List<Map<String, Object>> allList = new ArrayList();
        Long relationId = Long.valueOf(param.get("relationId"));
        req.put("relationId",relationId);
        //乘客事务录入明细表【现金事务】
        List<Map<String, Object>> swStInputPassengerWorkCash = swStInputPassengerWorkCashMapper.getSwStInputPassengerWorkCash(req);
        //乘客事务录入明细表【电子事务】
        List<Map<String, Object>> swStInputPassengerWorkElectron = swStInputPassengerWorkElectronMapper.getSwStInputPassengerWorkElectron(req);
        //乘客事务录入明细表【付费出站票发售】
        List<Map<String, Object>> swStInputPassengerWorkPay = swStInputPassengerWorkPayMapper.getSwStInputPassengerWorkPay(req);
        //乘客事务录入明细表【特殊车票发售】
        List<Map<String, Object>> swStInputPassengerWorkSpecial = swStInputPassengerWorkSpecialMapper.getSwStInputPassengerWorkSpecial(req);
        //乘客事务录入明细表【免费出站票发售】
        List<Map<String, Object>> swStInputPassengerWorkFree = swStInputPassengerWorkFreeMapper.getSwStInputPassengerWorkFree(req);
        //乘客事务录入明细表【故障退款】
        List<Map<String, Object>> swStInputPassengerWorkRefund = swStInputPassengerWorkRefundMapper.getSwStInputPassengerWorkRefund(req);
        req.clear();
        req.put("handlePersonCode",String.valueOf(param.get("handlePersonCode")));
        req.put("handleTime",String.valueOf(param.get("handleTime")));
        req.put("lineCode",String.valueOf(param.get("lineCode")));
        req.put("stationCode",String.valueOf(param.get("stationCode")));
        //乘客事务退换票
        List<Map<String, Object>> swStInputPassengerWorkTicket = swStInputPassengerWorkTicketMapper.getSwStInputPassengerWorkTicket(req);
        req.put("conductorCode",String.valueOf(param.get("handlePersonCode")));
        req.put("inputTime",String.valueOf(param.get("handleTime")));
        //其他相关数据录入
        List<Map<String, Object>> swStInputPassengerWorkOther = swStInputPassengerWorkOtherMapper.getSwStInputPassengerWorkOther(req);
        allList.addAll(swStInputPassengerWorkCash);
        allList.addAll(swStInputPassengerWorkElectron);
        allList.addAll(swStInputPassengerWorkPay);
        allList.addAll(swStInputPassengerWorkSpecial);
        allList.addAll(swStInputPassengerWorkFree);
        allList.addAll(swStInputPassengerWorkRefund);
        allList.addAll(swStInputPassengerWorkTicket);
        allList.addAll(swStInputPassengerWorkOther);
        int count = 0;
        for(Map<String, Object> m : allList){
            String auditStatus = String.valueOf(m.get("auditStatus"));
            if(!"2".equals(auditStatus)){
                count++;
                break;
            }
        }
        if(null != param.get("ids")){
            List<Integer> ids = (List) JSONObject.parseArray(String.valueOf(param.get("ids")));
            if(0==count){//子表所有数据都是通过时修改主表状态
                SwStInputPassengerWork swStInputPassengerWork = new SwStInputPassengerWork();
                swStInputPassengerWork.setId(relationId);
                swStInputPassengerWork.setAuditStatus("2");
                swStInputPassengerWorkMapper.updateById(swStInputPassengerWork);
            }
            String type = String.valueOf(param.get("type"));
            for(Integer id : ids){
                this.updateById(Long.valueOf(id),type,"2");
            }
        }
        Result res=new Result(ResultDetailMsg.OK);
        return res;
    }

    private void updateById(Long id,String type,String auditStatus){
        switch (type) {
            case "1": //乘客事务录入明细表【现金事务】
                SwStInputPassengerWorkCash swStInputPassengerWorkCash1 = new SwStInputPassengerWorkCash();
                swStInputPassengerWorkCash1.setId(Long.valueOf(id));
                swStInputPassengerWorkCash1.setAuditStatus(auditStatus);
                swStInputPassengerWorkCashMapper.updateById(swStInputPassengerWorkCash1);
                break;
            case "2": //乘客事务录入明细表【电子事务】
                SwStInputPassengerWorkElectron swStInputPassengerWorkElectron1 = new SwStInputPassengerWorkElectron();
                swStInputPassengerWorkElectron1.setId(Long.valueOf(id));
                swStInputPassengerWorkElectron1.setAuditStatus(auditStatus);
                swStInputPassengerWorkElectronMapper.updateById(swStInputPassengerWorkElectron1);
                break;
            case "3": //乘客事务录入明细表【付费出站票发售】
                SwStInputPassengerWorkPay swStInputPassengerWorkPay1 = new SwStInputPassengerWorkPay();
                swStInputPassengerWorkPay1.setId(Long.valueOf(id));
                swStInputPassengerWorkPay1.setAuditStatus(auditStatus);
                swStInputPassengerWorkPayMapper.updateById(swStInputPassengerWorkPay1);
                break;
            case "4": //乘客事务录入明细表【特殊车票发售】
                SwStInputPassengerWorkSpecial swStInputPassengerWorkSpecial1 = new SwStInputPassengerWorkSpecial();
                swStInputPassengerWorkSpecial1.setId(Long.valueOf(id));
                swStInputPassengerWorkSpecial1.setAuditStatus(auditStatus);
                swStInputPassengerWorkSpecialMapper.updateById(swStInputPassengerWorkSpecial1);
                break;
            case "5": //乘客事务录入明细表【免费出站票发售】
                SwStInputPassengerWorkFree swStInputPassengerWorkFree1 = new SwStInputPassengerWorkFree();
                swStInputPassengerWorkFree1.setId(Long.valueOf(id));
                swStInputPassengerWorkFree1.setAuditStatus(auditStatus);
                swStInputPassengerWorkFreeMapper.updateById(swStInputPassengerWorkFree1);
                break;
            case "6": //乘客事务录入明细表【故障退款】
                SwStInputPassengerWorkRefund swStInputPassengerWorkRefund1 = new SwStInputPassengerWorkRefund();
                swStInputPassengerWorkRefund1.setId(Long.valueOf(id));
                swStInputPassengerWorkRefund1.setAuditStatus(auditStatus);
                swStInputPassengerWorkRefundMapper.updateById(swStInputPassengerWorkRefund1);
                break;
            case "7": //乘客事务退换票
                SwStInputPassengerWorkTicket swStInputPassengerWorkTicket1 = new SwStInputPassengerWorkTicket();
                swStInputPassengerWorkTicket1.setId(Long.valueOf(id));
                swStInputPassengerWorkTicket1.setAuditStatus(auditStatus);
                swStInputPassengerWorkTicketMapper.updateById(swStInputPassengerWorkTicket1);
                break;
            case "8": //其他相关数据录入
                SwStInputPassengerWorkOther swStInputPassengerWorkOther1 = new SwStInputPassengerWorkOther();
                swStInputPassengerWorkOther1.setId(Long.valueOf(id));
                swStInputPassengerWorkOther1.setAuditStatus(auditStatus);
                swStInputPassengerWorkOtherMapper.updateById(swStInputPassengerWorkOther1);
                break;
        }
    }
}
